package zhihui.mobile.manager.shop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhihui.mobile.bean.page.PageInfo;
import zhihui.mobile.bean.shop.BannerInfo;
import zhihui.mobile.bean.shop.CategoryShopInfo;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.bean.shop.cart.CartInfo;
import zhihui.mobile.bean.shop.detail.ShopDetail;
import zhihui.mobile.bean.shop.order.AddOrderInfo;
import zhihui.mobile.bean.shop.order.OrderShopInfo;
import zhihui.mobile.bean.shop.sales.SalesInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.AllManagerObvs;
import zhihui.mobile.manager.IManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.net.HttpUtils;

/**
 * Created by 张公铭 on 2017/7/26 0026.
 */

public class ShopManager implements IManager
{
    private List<BannerInfo> shopMainBanner = new ArrayList<>();    //首页banner
    private List<BannerInfo> shopMainGroup = new ArrayList<>();     //首页团购
    private List<BannerInfo> shopMainTopic = new ArrayList<>();     //首页专题
    private PageInfo curPageInfo = null;                  //推荐当前页信息
    private List<SpuInfo> recommendList = new ArrayList<>();
    private SalesInfo qgSales = new SalesInfo();    //抢购
    private SalesInfo msSales = new SalesInfo();    //秒杀
    private CategoryShopInfo categoryShopInfo = new CategoryShopInfo();
    private CartInfo cartInfo = new CartInfo();     //购物车信息
    private ShopDetail curShop = new ShopDetail();  //当前商品详情


    private static ShopManager manager;

    private ShopManager()
    {
        AllManagerObvs.getInstance().addManager(this);
    }

    private static synchronized void createdIns()
    {
        if (manager == null)
        {
            manager = new ShopManager();
        }
    }

    public static ShopManager getInstance()
    {
        if (manager == null)
        {
            createdIns();
        }
        return manager;
    }

    public List<SkuInfo> getSelectSku()
    {
        List<SkuInfo> list = new ArrayList<>();
        for (SkuInfo skuInfo : cartInfo.getSkus())
        {
            if (skuInfo.isSelect())
            {
                list.add(skuInfo);
            }
        }
        return list;
    }

    public List<BannerInfo> getShopMainBanner()
    {
        return shopMainBanner;
    }

    public List<BannerInfo> getShopMainGroup()
    {
        return shopMainGroup;
    }

    public List<BannerInfo> getShopMainTopic()
    {
        return shopMainTopic;
    }

    public void setShopMainBanner(List<BannerInfo> list)
    {
        if (shopMainBanner == null)
        {
            shopMainBanner = new ArrayList<>();
        }
        shopMainBanner.clear();
        shopMainBanner.addAll(list);
    }

    public void setShopMainGroup(List<BannerInfo> list)
    {
        if (shopMainGroup == null)
        {
            shopMainGroup = new ArrayList<>();
        }
        shopMainGroup.clear();
        shopMainGroup.addAll(list);
    }

    public void setShopMainTopic(List<BannerInfo> list)
    {
        if (shopMainTopic == null)
        {
            shopMainTopic = new ArrayList<>();
        }
        shopMainTopic.clear();
        shopMainTopic.addAll(list);
    }

    public CategoryShopInfo getCategoryShopInfo()
    {
        return categoryShopInfo;
    }

    public void setCategoryShopInfo(CategoryShopInfo categoryShopInfo)
    {
        this.categoryShopInfo = categoryShopInfo;
    }

    public void setQgSales(SalesInfo qgSales)
    {
        this.qgSales = qgSales;
    }

    public void setMsSales(SalesInfo msSales)
    {
        this.msSales = msSales;
    }

    public SalesInfo getQgSales()
    {
        return qgSales;
    }

    public SalesInfo getMsSales()
    {
        return msSales;
    }

    public CartInfo getCartInfo()
    {
        return cartInfo;
    }

    public void setCartInfo(CartInfo cartInfo)
    {
        this.cartInfo = cartInfo;
    }

    public void setCurPageInfo(PageInfo curPageInfo)
    {
        this.curPageInfo = curPageInfo;
    }

    public PageInfo getCurPageInfo()
    {
        return curPageInfo;
    }

    public void setRecommendList(List<SpuInfo> list)
    {
        if (recommendList == null)
        {
            recommendList = new ArrayList<>();
        }
        this.recommendList.addAll(list);
    }

    public List<SpuInfo> getRecommendList()
    {
        return recommendList;
    }

    public ShopDetail getCurShop()
    {
        return curShop;
    }

    public void setCurShop(ShopDetail curShop)
    {
        this.curShop = curShop;
    }

    /**
     * 初始化shop 首页
     */
    public void initShopMain(IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.set.AppSet");
        params.put("method", "findImg");

        HttpUtils.postAes(Config.URL, params, null, listener);
    }

    public void initShopMainSales(IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.activitie.Sale");
        params.put("method", "findSaleNow");

        HttpUtils.postAes(Config.URL, params, null, listener);
    }

    /**
     * 获取首页推荐
     */
    public void initShopMianRecommend(IHttpCallBackListener listener, int page, int pageCount)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.goods.GoodsSpu");
        params.put("method", "recommended");

        List<Object> list = new ArrayList<>();
        list.add(page);
        list.add(pageCount);

        HttpUtils.postAes(Config.URL, params, list, listener);
    }


    public void addShopInCart(IHttpCallBackListener listener, int skuId, int num)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "joinCart");

        List<Object> list = new ArrayList<>();
        list.add(UserManager.getInstance().getCurUser().getId());
        list.add(skuId);
        list.add(num);

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * @param listener
     * @param param    关键字
     * @param page     页数
     * @param descType 排序类型如果为空 默认 综合排序，1：销量排序，2：价格排序
     * @param desc     如果为空 默认 升序， "desc" 降序， "asc" 升序
     */
    public void getShopListByCategoryId(IHttpCallBackListener listener, String param, int page, String descType, String desc)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.goods.GoodsSpu");
        params.put("method", "findByCategory");

        List<Object> list = new ArrayList<>();
        list.add(page);
        list.add(10);
        list.add(param);
        list.add(descType);
        list.add(desc);

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 获取购物车商品列表
     */
    public void getCartShopList(IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "findSkuByUserId");


        List<Object> list = new ArrayList<>();
        list.add(UserManager.getInstance().getCurUser().getId());

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * @param listener
     * @param param    关键字
     * @param page     页数
     * @param descType 排序类型如果为空 默认 综合排序，1：销量排序，2：价格排序
     * @param desc     如果为空 默认 升序， "desc" 降序， "asc" 升序
     */
    public void getShopListBySearch(IHttpCallBackListener listener, String param, int page, String descType, String desc)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.goods.Search");
        params.put("method", "search");

        List<Object> list = new ArrayList<>();
        list.add(page);
        list.add(10);
        list.add(param);
        list.add(descType);
        list.add(desc);

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 批量删除购物车
     *
     * @param listener
     * @param list
     */
    public void deleteCart(IHttpCallBackListener listener, List<Object> list)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "batchDelSkuInCart");

        HttpUtils.postAes(Config.URL, params, list, listener);
    }


    /**
     * 创建订单成功刷新购物车列表
     */
    public void refreCartShopList(AddOrderInfo addOrderInfo)
    {
        List<SkuInfo> newList = new ArrayList<>();
        boolean isexist = false;    //是否存在订单中
        for (SkuInfo skuInfo : cartInfo.getSkus())
        {
            isexist = false;
            for (OrderShopInfo info : addOrderInfo.getList())
            {
                if (info.getSkuId() == skuInfo.getSkuId())
                {
                     cartInfo.setCartNum(cartInfo.getCartNum()-info.getSkuNum());
                    isexist = true;
                    break;
                }
            }
            if (!isexist)
            {
                newList.add(skuInfo);
            }
        }
        cartInfo.setSkus(newList);
    }

    /**
     * 修改购物车数量
     */
    public void updateCartNum(IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "updateNum");

        List<Object> list = new ArrayList<>();
        list.add(UserManager.getInstance().getCurUser().getId());
        StringBuffer id_sb = new StringBuffer();
        StringBuffer num_sb = new StringBuffer();

        List<SkuInfo> skuList = cartInfo.getSkus();
        for (int i = 0; i < skuList.size(); i++)
        {
            id_sb.append(skuList.get(i).getSkuId());
            if (i < skuList.size() - 1)
            {
                id_sb.append(",");
            }

            num_sb.append(skuList.get(i).getNum());
            if (i < skuList.size() - 1)
            {
                num_sb.append(",");
            }
        }

        list.add(id_sb.toString());
        list.add(num_sb.toString());

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 获取商品详情
     */
    public void getShopDetailsByShopId(IHttpCallBackListener listener, int shopId, int salesId)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.goods.GoodsSpu");
        params.put("method", "details");

        List<Object> list = new ArrayList<>();
        list.add(UserManager.getInstance().getCurUser() == null ? "" : UserManager.getInstance().getCurUser().getId());
        list.add(shopId);
        list.add(salesId);

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    @Override
    public void clear()
    {
        AllManagerObvs.getInstance().removeManager(this);
        shopMainBanner.clear();
        shopMainGroup.clear();
        shopMainTopic.clear();
        recommendList.clear();

        shopMainTopic = null;
        shopMainGroup = null;
        shopMainBanner = null;
        recommendList = null;
        curPageInfo = null;
        categoryShopInfo = null;

        manager = null;
    }
}
